C++Builder Hack

Brian Long (www.blong.com)

[ This post was originally made on a weblog in June 2004. Consequently, some of the comments about upcoming technologies have come and gone... ]

I’ve been a Borland C++Builder user ever since it first came out in February 1997, exactly two years after Delphi’s release. A really nice feature is that a C++Builder project can be made out of both C++ units and Delphi units. Mixed language development is catered for in C++Builder.

The drawback with this panacea has always been that whilst you can edit C++ forms, drop components on them, make event handlers etc., you cannot do the same with Delphi forms added to a C++Builder project. You can edit the code, but there is no designer support. This always seemed an arbitrary limitation; it would make for great cross-language development to be able to fully edit C++ and Delphi forms in one IDE.

For years I’ve worked on the basis that this was achieved through an ifdef in the source code base, but actively spelunked in the hope that one day I would find a magic switch that could reverse the situation. And that day is now. I have found the magic switch to enable Delphi form editing in C++Builder, giving a proper multi-language (or should that be bi-language) development environment!

Well, not a proper multi-language development environment as such. The File | New | Other... dialog does not have, by default any options for adding Delphi forms to projects, but I guess that wouldn’t be too difficult to rectify by adding a simple form to the Object Repository. Also Delphi units can be created by making a new text file, saving it as a Pascal unit (that option is in the File | Save As... dialog’s file type list) and inserting a suitable Code Template (Ctrl+J) into it.

So what is the magic switch, you ask? It turns out that in both Delphi 6 and C++Builder 6 there is a package called coride60.bpl that contains compiled versions of various units that control the behaviour of, you guessed it, the core part of the IDE. Careful browsing through the exports from this package reveals a unit called PasMgr, which exposes an intriguing variable called AllowFormEdits. Examination shows this variable is True in Delphi but False in C++Builder. Enabling Delphi form editing amounts to the “simple” job of setting it to True in C++Builder.

When a package exports a variable using a mangled symbol (@PasMgr@AllowFormEdits in this case) the address of this exported symbol contains the address of the corresponding variable, so a unit like the following one will access it. Just add it a new package in C++Builder 6 and away you go.


unit Hack;

{$ifndef VER140}
{$error 'This only works with C++Builder 6'}
{$endif}

interface

procedure Register;

implementation

uses
  Windows, SysUtils;

const
  PackageName = 'coreide60.bpl';
  AllowFormEditsName = '@Pasmgr@AllowFormEdits';

var
  PackageHandle: HModule = 0;
  AllowFormEdits: PBoolean = nil;

procedure EnableFormEditing;

  procedure ConnectToPackageExport(var ProcVar; const ProcName: PChar);
  begin
    @TProcedure(ProcVar) := GetProcAddress(PackageHandle, ProcName);
    if not Assigned(TProcedure(ProcVar)) then
      raise Exception.CreateFmt('Cannot locate exported symbol %s', [ProcName]);
  end;

begin
  if PackageHandle = 0 then
  begin
    PackageHandle := GetModuleHandle(PackageName);
    if PackageHandle = 0 then
      raise Exception.CreateFmt('Unable to locate %s package in memory', [PackageName]);
    ConnectToPackageExport(AllowFormEdits, AllowFormEditsName);
  end;
  AllowFormEdits^ := True;
end;

procedure Register;
begin
  EnableFormEditing
end;

end.

Before leaving this subject I should mention that it has been made clear that plans are afoot to allow development of both Delphi for Win32 and Delphi for .NET projects in the next Delphi release, which online posts suggest people are guessing will be called something along the lines of “Delphi 9”. You can see this announcement made by Michael Swindell, Director of Developer Tools - Delphi and .NET, at http://bdn.borland.com/article/0,1410,32024,00.html, where he says: Our plans include moving Win32 support into the "Galileo" IDE along side Delphi .NET in the next major release - which is planned to combine both Win32 and .NET features into the same IDE.

[ Of course this product ended up being released as Delphi 2005 in October 2004. ]

In some senses this would seem to imply another mixed mode development environment, although more in the vein of Borland Pascal 7.0. In that ancient product you could write Pascal code in projects that targeted DOS, DPMI or 16-bit Windows. It seems the next Delphi release will allow you to write Delphi code that targets 32-bit Windows or .NET (although the language support offered by the respective compilers is a little different between the platforms). Interesting how things cycle around...

What would be a great addition is to allow C++Builder forms to be developed in the IDE. It is clearly quite a reasonable task, as C++Builder 6.0 already supports this ability and would assure the C++Builder users who have wondered about their future with the release of C++BuilderX and the lack of the promised Open Letter (see http://homepages.borland.com/aohlsson/blog_beta/archive/2004_05_02_archive#108403199207353987 and http://216.101.185.148/scripts/isapi.dll/article?id=34C1D7BA&article=5205265) that they have not actually been abandoned.

[ Well, the Open Letter eventually arrived on December 2004 (see http://bdn.borland.com/article/0,1410,32845,00.html), and we now know the next version of Delphi, currently code-named DeXter, will add C++Builder functionality into the mix over and above the Delphi Win32, Delphi .NET and C# functionality in Delphi 2005.]

Thanks to Mzwanele for pointers to getting what I wanted to happen to happen.

About Brian Long

Brian Long used to work at Borland UK, performing a number of duties including Technical Support on all the programming tools. Since leaving in 1995, Brian has spent the intervening years as a trainer, trouble-shooter and mentor focusing on the use of the C#, Delphi and C++ languages, and of the Win32 and .NET platforms. In his spare time Brian actively researches and employs strategies for the convenient identification, isolation and removal of malware.

If you need training in these areas or need solutions to problems you have with them, please get in touch or visit Brian's Web site.

Brian authored a Borland Pascal problem-solving book in 1994 and occasionally acts as a Technical Editor for Wiley (previously Sybex); he was the Technical Editor for Mastering Delphi 7 and Mastering Delphi 2005 and also contributed a chapter to Delphi for .NET Developer Guide. Brian is a regular columnist in The Delphi Magazine and has had numerous articles published in Developer's Review, Computing, Delphi Developer's Journal and EXE Magazine. He was nominated for the Spirit of Delphi 2000 award.


Back to top